package com.yanfan.modbus.frame;

import com.yanfan.modbus.code.FunctionCode;
import com.yanfan.modbus.exception.ModbusTransportException;
import com.yanfan.modbus.frame.abs.ModbusResponse;
import io.netty.buffer.ByteBuf;


public class WriteRegisterResponse extends ModbusResponse {
    private int writeOffset;
    private int writeValue;


    public WriteRegisterResponse(int slaveId) throws ModbusTransportException {
        super(slaveId);
    }

    public WriteRegisterResponse(int slaveId, int writeOffset, int writeValue) throws ModbusTransportException {
        super(slaveId);
        this.writeOffset = writeOffset;
        this.writeValue = writeValue;
    }


    @Override
    public byte getFunctionCode() {
        return FunctionCode.WRITE_REGISTER;
    }


    @Override
    protected void writeResponse(ByteBuf buf) {
        buf.writeShort(writeOffset);
        buf.writeShort(writeValue);
    }


    @Override
    protected void readResponse(ByteBuf buf) {
        writeOffset = buf.readUnsignedShort();
        writeValue = buf.readUnsignedShort();
    }


    public int getWriteOffset() {
        return writeOffset;
    }


    public int getWriteValue() {
        return writeValue;
    }
}
